package com.me.study.ui.activity.blue

import android.Manifest
import android.annotation.SuppressLint
import android.bluetooth.BluetoothDevice
import android.os.Bundle
import android.view.View
import com.blankj.utilcode.util.ToastUtils
import com.me.study.R
import com.me.study.app.base.BaseActivity
import com.me.study.app.ext.goActivity
import com.me.study.app.laserRange.ACSUtility
import com.me.study.app.laserRange.DiffuseView
import com.me.study.databinding.ActivitySearchBluetoothBinding
import com.permissionx.guolindev.PermissionX
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel


class SearchBluetoothActivity : BaseActivity<BaseViewModel, ActivitySearchBluetoothBinding>(),
    View.OnClickListener, DiffuseView.onViewClickListener {

    private lateinit var acsUtility: ACSUtility
    private var mSelectedPort: ACSUtility.blePort? = null

    override fun layoutId() = R.layout.activity_search_bluetooth

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.btnOpen.setOnClickListener(this)
        mDatabind.btnStart.setOnClickListener(this)
        mDatabind.ripple.setOnBlueViewListener(this)
    }

    override fun onResume() {
        super.onResume()
        PermissionX.init(this)
            .permissions(
                Manifest.permission.BLUETOOTH_SCAN,
                Manifest.permission.BLUETOOTH_CONNECT,
                Manifest.permission.BLUETOOTH_ADMIN,
                Manifest.permission.BLUETOOTH,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
            ).request { allGranted, grantedList, deniedList ->
                if (allGranted) {
                    acsUtility = ACSUtility(this, ACSCallback())
                }
            }
    }

    override fun onClick(p0: View) {
        when (p0.id) {
            R.id.btn_open -> {
                mSelectedPort?.let {
                    acsUtility.openPort(it)
                    mDatabind.btnOpen.isEnabled = false
                }
            }
            R.id.btn_start -> {
                mDatabind.ripple.start()
                acsUtility.enumAllPorts(10f)
                mDatabind.btnStart.isEnabled = false
                mDatabind.btnOpen.text = "连接"
            }
        }
    }

    inner class ACSCallback : ACSUtility.IACSUtilityCallback {

        override fun utilReadyForUse() {
            mDatabind.btnStart.isEnabled = true
        }

        @SuppressLint("MissingPermission")
        override fun didFoundPort(newPort: ACSUtility.blePort?) {
            runOnUiThread {
                newPort?.let {
                    if (it._device.name.contains("LDM")) {
                        mDatabind.ripple.addRandomDevice(newPort)
                    }
//                    mDatabind.ripple.addRandomDevice(newPort)
                }
            }
        }

        override fun didFinishedEnumPorts() {
            mDatabind.btnStart.isEnabled = true
            mDatabind.ripple.pause()
        }

        override fun didOpenPort(port: ACSUtility.blePort, bSuccess: Boolean) {
            when (bSuccess) {
                true -> {
                    mDatabind.ripple.pause()
                    acsUtility.stopEnum()
                    val b = Bundle()
                    b.putParcelable(BluetoothDevice.EXTRA_DEVICE, port)
                    goActivity(OperateBluetoothActivity::class.java, b)
                    this@SearchBluetoothActivity.finish()
                }
                false -> {
                    ToastUtils.showShort("连接失败!")
                    mDatabind.btnOpen.isEnabled = true
                }
            }
        }

        override fun didClosePort(port: ACSUtility.blePort?) {

        }

        override fun didPackageSended(succeed: Boolean) {

        }

        override fun didPackageReceived(port: ACSUtility.blePort, packageToSend: ByteArray) {

        }

        override fun heartbeatDebug() {

        }

    }

    @SuppressLint("MissingPermission")
    override fun onBlueClick(deviceBlue: ACSUtility.blePort) {
        mSelectedPort = deviceBlue
        mDatabind.btnOpen.isEnabled = true
        mDatabind.btnOpen.text = "连接${deviceBlue._device.name}"
    }

}